import 'package:flutter/material.dart';
import 'package:flutter_yishop/constants/Constants.dart';
import 'package:flutter_yishop/pages/shopcar/CartItem.dart';
import 'package:flutter_yishop/provider/Cart.dart';
import 'package:flutter_yishop/services/CartServices.dart';
import 'package:flutter_yishop/services/UserService.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';
import 'package:flutter_yishop/utils/toast_util.dart';
import 'package:provider/provider.dart';

class ShopCar extends StatefulWidget {

  bool isBack ;

  ShopCar({this.isBack= true,Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ShopCar();
  }
}

class _ShopCar extends State<ShopCar> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    var cartProvider = Provider.of<Cart>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("购物车"),
        elevation: 0,
        automaticallyImplyLeading: widget.isBack,
        actions: [IconButton(onPressed: () {
      cartProvider.allPrice == 0?ToastUtil.showToasts("请选择商品再删除！"): showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    title: Text("提示"),
                    content: Text("确定要删除吗"),
                    actions: <Widget>[
                      FlatButton(
                          child: Text("取消"),
                          onPressed: () => Navigator.pop(context, "cancel")),
                      FlatButton(
                          child: Text("确定"),
                          onPressed: ()  {
                          cartProvider.removeItem();
                          Navigator.pop(context, "yes");
                          }),
                    ],
                  );
                });
          }, icon: Icon(Icons.delete))],
      ),
      body: cartProvider.cartList.length > 0
          ? Stack(
              children: [
                Container(
                  margin: EdgeInsets.only(bottom: ScreenAdapter.height(78)),
                  child: ListView(
                    children: cartProvider.cartList.map((value) {
                      return CartItem(value);
                    }).toList(),
                  ),
                ),
                Positioned(
                  bottom: 0,
                  width: ScreenAdapter.width(750),
                  height: ScreenAdapter.height(78),
                  child: Container(
                    decoration: BoxDecoration(
                      border: Border(
                          top: BorderSide(width: 1, color: Colors.black12)),
                      color: Colors.white,
                    ),
                    width: ScreenAdapter.width(750),
                    height: ScreenAdapter.height(78),
                    child: Stack(
                      children: <Widget>[
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Row(
                            children: <Widget>[
                              Container(
                                width: ScreenAdapter.width(60),
                                child: Checkbox(
                                  value: cartProvider.isCheckedAll,
                                  activeColor: Colors.pink,
                                  onChanged: (val) {
                                    setState(() {
                                      cartProvider.checkAll(val);
                                    });
                                  },
                                ),
                              ),
                              Text("全选"),
                              Container(
                                margin: EdgeInsets.only(left: ScreenAdapter.width(20)),
                                child: Row(
                                  children: [
                                    Text("总价："),
                                    Text("￥${cartProvider.allPrice}",
                                    style: TextStyle(
                                      color: Colors.redAccent
                                    ),)
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: RaisedButton(
                            child: Text("结算",
                                style: TextStyle(color: Colors.white)),
                            color: Colors.red,
                            onPressed: () {
                              // CartServices.getCheckOutData().length>0?Navigator.of(context).pushNamed(Constants.CHECKOUT):ToastUtil.showToasts("请选中商品");
                              if(UserService.getUserLoginState()==true){
                                CartServices.getCheckOutData().length>0?Navigator.of(context).pushNamed(Constants.CHECKOUT):ToastUtil.showToasts("请选中商品");
                              }else{
                                Navigator.of(context).pushNamed(Constants.LOGIN);
                              }
                            },
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            )
          : Center(
              child: Text("购物车空空的"),
            ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
